import { Toast,Badge  } from 'antd-mobile';
import {growthWeekly, growthWeeklyContent, shopWeekly, shopWeeklyContent, shopWeeklySearch } from '../services/knowledge';

export default {
  namespace: 'knowledge',
  state: {
    tabActiveKey:'1',
    active1:'孕期',
    growthWeekly:[],
    growthWeeklyContent:{},
    active2:'奶粉',
    shopWeekly:[],
    shopWeeklyContent:{},
    recommend:[],
  },
  reducers: {
    change(state, { payload }) {
      return {...state, ...payload};
    },
  },
  effects: {

    *growthWeekly({ payload }, { put, call, select }){

      let _growthWeekly=yield select(state=>state.knowledge.growthWeekly);

      if(!_growthWeekly.length){
        let {data}=yield call(growthWeekly);
        yield put({
          type: 'change',
          payload: {
            growthWeekly:data,
          },
        });
      }

    },

    *growthWeeklyContent({ payload }, { put, call, select }){

      let {data}=yield call(growthWeeklyContent, payload.id);
      yield put({
        type: 'change',
        payload: {
          growthWeeklyContent:data,
        },
      });
    },



    *shopWeekly({ payload }, { put, call, select }){

      let _shopWeekly=yield select(state=>state.knowledge.shopWeekly);

      if(!_shopWeekly.length){
        let {data}=yield call(shopWeekly);
        yield put({
          type: 'change',
          payload: {
            shopWeekly:data,
          },
        });
      }

    },

    *shopWeeklyContent({ payload }, { put, call, select }){

      let {data}=yield call(shopWeeklyContent, payload.id);
      yield put({
        type: 'change',
        payload: {
          shopWeeklyContent:data,
        },
      });
    },

    *shopWeeklySearch({ payload }, { put, call, select }){

      let {data}=yield call(shopWeeklySearch, payload.key);
      console.log(data)
      yield put({
        type: 'change',
        payload: {
          recommend:data,
        },
      });
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/knowledgeBase') {
          dispatch({ type: 'growthWeekly' });
          dispatch({ type: 'shopWeekly' });
        }
        else if (pathname === '/knowledgeInfo') {
          dispatch({ type: 'growthWeeklyContent',payload:{id:query.id} });
        }
        else if (pathname === '/shopWeeklyInfo') {
          dispatch({ type: 'shopWeeklyContent',payload:{id:query.id} });
        }
      });
    },
  },
};
